<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Comment.php 16298 2019-01-28 14:12:48 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_comment 评论
 * +----------------------------------------------------------------------
 */

namespace application\comment\controller;

use application\home\controller\auth;
use ticky\request;

class index extends auth {

    // 首页
    public function index() {
        if (submitcheck("dosubmit")) {
            $arr = array();
            $arr['upid'] = request::get('upid', '0');
            $arr['uid'] = $this->uid;
            $arr['idtype'] = request::get('idtype', 'content');
            $arr['id'] = request::get('id', '0');
            $arr['message'] = $this->_recontent($_POST['content']);
            $arr['ip'] = $this->ip;
            $arr['status'] = request::post('status', '1');
            $arr['dateline'] = time();
            //如果是回复
            if ($_POST['reply']) {
                $arr['upid'] = request::get('reply', '0');
                //查询之前的评论内容,并整理楼层样式
                $r = db('comment')->field('uid,message')->where(array('cid' => $arr['upid']))->find();
                if ($r) {
                    $username = db('member')->field('username')->where(array('uid' => $_POST['reply_to']))->one();
                    $str = strstr($r['message'], 'original_comment') ? '<span class="original_comment">' . $r['message'] . ' </span>' : '<span class="original_comment"><a href="javascript:void(0);" class="user_name" rel="nofollow">' . $username . '</a> : ' . $r['message'] . ' </span>';
                } else {
                    $str = '';
                }
                //这里的格式不可以修改，会影响到后台的评论展示列表
                $arr['message'] = $str . '<a href="javascript:void(0);" class="user_name" rel="nofollow">' . $username . '</a>：' . $_POST['content'];
            }
            db('comment')->insert($arr);
            showmsg('评论成功', '/comment/index/index/?id=' . $arr['id'] . '&idtype=content');
        } else {
            $arr = array();
            $arr['idtype'] = request::get('idtype', 'content');
            $arr['id'] = request::get('id', '0');
            $arr['upid'] = request::get('upid', '0');
            $ret = db('comment')->where($arr)->newpage();
            foreach ($ret['items'] as $row) {
                $username = db('member')->field('username')->where(array('uid' => $row['uid']))->one();
                if ($username) {
                    $row['username'] = $username;
                } else {
                    $row['username'] = "游客";
                }

                $childs = db('comment')->where(['upid' => $row['cid']])->select();
                foreach ($childs as $child) {
                    $row['childs'] .= '<p>' . $child['message'] . '</p>';
                }
                $data[] = $row;
            }
            $this->assign('page', $ret['page']);
            $this->assign('items', $data);
            $this->assign('items_count', count($ret['items']));
            $this->display('addcomment');
        }
    }

    //处理内容
    protected function _recontent($content) {
        $content = preg_replace('[\[em_([0-9]*)\]]', '<img src="' . STATIC_URL . 'images/face/$1.gif"/>', $content);
        $arr = explode('|', get_config('prohibit_words'));
        return str_replace($arr, '*', $content);
    }

}
